import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib import cm
from math import log
# Mesh grid demo
x_bm = np.linspace(start=-3, stop=3, num=7)
y_bm = np.linspace(start=-3, stop=3, num=7)
x_bm
y_bm
#Plot before meshgrid
plt.figure(figsize=[10, 8])
plt.scatter(x_bm, y_bm, s = 100, color = 'blue', alpha = 0.5)
plt.ylim(-3.1, 3.1)
plt.xlim(-3.1, 3.1)
plt.show()
x_bm.ndim
y_bm.ndim
x_am, y_am = np.meshgrid(x_bm, y_bm)
print('Array after meshgrid: ', x_am.shape)
x_am.ndim
y_am.ndim
# Plotting the mesh_grid
plt.figure(figsize=[10, 8])
plt.scatter(x_am, y_am, s = 100, color = 'blue', alpha = 0.5)
plt.xlabel('X', fontsize = 30)
plt.ylabel('Y', fontsize = 30)
plt.ylim(-3.1, 3.1)
plt.xlim(-3.1, 3.1)
plt.show()
def f(x, y):
r = 3**(-x**2 - y**2)
return 1 / (r + 1)
x_4 = np.linspace(start=-2, stop=2, num=200)
y_4 = np.linspace(start=-2, stop=2, num=200)
x_4.shape
x_4.ndim
y_4.shape
x_4, y_4 = np.meshgrid(x_4, y_4)
print('Array after meshgrid: ', x_4.shape)
x_4.ndim
y_4.ndim
fig = plt.figure(figsize=[16, 12])
ax = fig.gca(projection = '3d')
ax.set_xlabel('X', fontsize=30)
ax.set_ylabel('Y', fontsize=30)
ax.set_zlabel('f(x, y) - Cost', fontsize=30)
plt.show()
x_4.shape
fig = plt.figure(figsize=[16, 12])
ax = fig.gca(projection = '3d')
ax.set_xlabel('X', fontsize=30)
ax.set_ylabel('Y', fontsize=30)
ax.set_zlabel('f(x, y) - Cost', fontsize=30)
ax.plot_surface(x_4, y_4, f(x_4, y_4), cmap=cm.coolwarm, alpha = 0.5)
plt.show()
def fpx(x,y):
r = 3**(-x**2 - y**2)
return 2*x*log(3)*r / (r+1)**2
def fpy(x, y):
r = 3**(-x**2 - y**2)
return 2*y*log(3)*r / (r + 1)**2
# the loop for the gradient descent
multiplier = 0.1
max_iter = 1000
params = np.array([1.8, 1.0])
params[0]
params[1]
print('The cost when I start is : ', f(params[0], params[1]))
for n in range(max_iter):
gradient_x = fpx(params[0], params[1])
gradient_y = fpy(params[0], params[1])
gradients = np.array([gradient_x, gradient_y])
params = params - multiplier * gradients
#Results
print('Values in gradient array', gradients)
print('Minimum occurs at x value of: ', params[0])
print('Minimum occurs at y value of: ', params[1])
print('The cost is: ', f(params[0], params[1]))
# The lines below are for explanation
params.shape
params.ndim
params
values_array = params.reshape(1,2)
values_array
values_array.shape
values_array.ndim
# The lines above are for explanation
multiplier = 0.1
max_iter = 1000
params = np.array([1.8, 1.0])
values_array = params.reshape(1,2)
for n in range(max_iter):
gradient_x = fpx(params[0], params[1])
gradient_y = fpy(params[0], params[1])
gradients = np.array([gradient_x, gradient_y])
params = params - multiplier * gradients
values_array = np.append(values_array, params.reshape(1, 2), axis=0)
# Results...
print('shape of params:', params.shape, '\n')
print('shape of values_array:', values_array.shape, '\n')
print('Printing Params:', params, '\n')
print('Printing Values_array:', '\n''\n', values_array,'\n')
print('Values in gradient array:', gradients, '\n')
print('Minimum occurs at x value of: ', params[0], '\n')
print('Minimum occurs at y value of: ', params[1],'\n')
print('The cost is: ', f(params[0], params[1]))
fig = plt.figure(figsize=[16, 12])
ax = fig.gca(projection='3d')
ax.set_xlabel('X', fontsize=20)
ax.set_ylabel('Y', fontsize=20)
ax.set_zlabel('f(x, y) - Cost', fontsize=20)
ax.plot_surface(x_4, y_4, f(x_4, y_4), cmap=cm.coolwarm, alpha=0.4)
ax.scatter(values_array[:, 0], values_array[:, 1],
f(values_array[:, 0], values_array[:, 1]), s=50, color='red')
plt.show()
# the MSE